package sun.cn.validate.holder;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.apache.commons.lang3.StringUtils;

public class ValidateHolder {

	private final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();

	private String name;


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	/**
	 * <p>数据校验器 返回null说明校验通过 否则输出错误信息</p>
	 * @author sunlingao
	 * @date 2017年7月25日
	 * @version
	 * @param obj
	 * @return
	 */
	public String validatorBean(Object obj){
		Set<ConstraintViolation<Object>> validators = VALIDATOR.validate(obj);
		if(validators != null && !validators.isEmpty()){
			for(ConstraintViolation<Object> var : validators){
				if(var != null && StringUtils.isNotBlank(var.getMessage()))
					return var.getMessage();
			}
		}
		return null;
	}


}
